#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
    static bool cmp(const vector<int> &a, const vector<int> &b){
        return a[1]<b[1];
    }
    int eraseOverlapIntervals(vector<vector<int>>& intervals) {
        sort(intervals.begin(), intervals.end(), cmp);

        int right = intervals[0][1];
        int cnt = 1;
        for(int i=1; i<intervals.size(); ++i){
            if(intervals[i][0] < right) continue;
            else{
                ++cnt;
                right = intervals[i][1];
            }
        }
        return intervals.size()-cnt;
    }
};